﻿using System;
using System.Collections.Generic;
//
using PerceptiveMCAPI;
//

namespace PerceptiveMCAPI_Test
{
    public partial class get_listBatchSubscribe : System.Web.UI.UserControl
    {
        public ApiMethod method { get; set; }

        public string listId
        {
            get { return tbListId.Text.Trim(); }
            set { tbListId.Text = value; }
        }
        public List<Dictionary<string, object>> batch
        {
            get { return GetBatch(); }
        }
        public bool double_optin
        {
            get { return cbDoubleOptin.Checked; }
            set { cbDoubleOptin.Checked = value; }
        }
        public bool replace_interests
        {
            get { return cbReplaceInterest.Checked; }
            set { cbReplaceInterest.Checked = value; }
        }
        public bool update_existing
        {
            get { return cbUpdateExisting.Checked; }
            set { cbUpdateExisting.Checked = value; }
        }

        // -----------------------------------------------------
        public bool Setup ()
        {
            this.Visible = true;
            lblSection.Text = method.section;
            lblMethod.Text = method.name;
            //
            double_optin = MCAPISettings.default_listSubscribe_double_optin;
            replace_interests = MCAPISettings.default_listSubscribe_replace_interests;
            update_existing = MCAPISettings.default_listSubscribe_update_existing;
            //
            ResetBatch();
            return true;
        }
        private List<Dictionary<string, object>> GetBatch ()
        {
            List<Dictionary<string, object>> batch = new List<Dictionary<string, object>>();
            //
            for ( int ix = 0; ix < 10; ix++ )
            {
                Dictionary<string, object> entry = new Dictionary<string, object>();
                //
                string email = ""; string email_type = ""; string merge_vars = "";
                switch ( ix )
                {
                    case 1:
                        email = txtEmail01.Text; email_type = txtEmailType01.Text; merge_vars = txtMergeVars01.Text;
                        break;
                    case 2:
                        email = txtEmail02.Text; email_type = txtEmailType02.Text; merge_vars = txtMergeVars02.Text;
                        break;
                    case 3:
                        email = txtEmail03.Text; email_type = txtEmailType03.Text; merge_vars = txtMergeVars03.Text;
                        break;
                    case 4:
                        email = txtEmail04.Text; email_type = txtEmailType04.Text; merge_vars = txtMergeVars04.Text;
                        break;
                    case 5:
                        email = txtEmail05.Text; email_type = txtEmailType05.Text; merge_vars = txtMergeVars05.Text;
                        break;
                    case 6:
                        email = txtEmail06.Text; email_type = txtEmailType06.Text; merge_vars = txtMergeVars06.Text;
                        break;
                    case 7:
                        email = txtEmail07.Text; email_type = txtEmailType07.Text; merge_vars = txtMergeVars07.Text;
                        break;
                    case 8:
                        email = txtEmail08.Text; email_type = txtEmailType08.Text; merge_vars = txtMergeVars08.Text;
                        break;
                    case 9:
                        email = txtEmail09.Text; email_type = txtEmailType09.Text; merge_vars = txtMergeVars09.Text;
                        break;
                    case 10:
                        email = txtEmail10.Text; email_type = txtEmailType10.Text; merge_vars = txtMergeVars10.Text;
                        break;
                }
                //
                if ( !string.IsNullOrEmpty( email ) )
                {
                    entry.Add( "EMAIL", email );
                    entry.Add( "EMAIL_TYPE", email_type );
                    string[] vars = merge_vars.Split( new string[] { "&" }, StringSplitOptions.RemoveEmptyEntries );
                    foreach ( string item in vars )
                    {
                        string[] kvp = item.Split( new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries );
                        entry.Add( kvp[0], kvp[1] );
                    }
                    batch.Add( entry );
                }
            }
            return batch;
        }
        private void ResetBatch ()
        {
            string email_type = MCAPISettings.default_listSubscribe_email_type.ToString();
            txtEmail01.Text = ""; txtEmailType01.Text = email_type; txtMergeVars01.Text = "";
            txtEmail02.Text = ""; txtEmailType02.Text = email_type; txtMergeVars02.Text = "";
            txtEmail03.Text = ""; txtEmailType03.Text = email_type; txtMergeVars03.Text = "";
            txtEmail04.Text = ""; txtEmailType04.Text = email_type; txtMergeVars04.Text = "";
            txtEmail05.Text = ""; txtEmailType05.Text = email_type; txtMergeVars05.Text = "";
            txtEmail06.Text = ""; txtEmailType06.Text = email_type; txtMergeVars06.Text = "";
            txtEmail07.Text = ""; txtEmailType07.Text = email_type; txtMergeVars07.Text = "";
            txtEmail08.Text = ""; txtEmailType08.Text = email_type; txtMergeVars08.Text = "";
            txtEmail09.Text = ""; txtEmailType09.Text = email_type; txtMergeVars09.Text = "";
            txtEmail10.Text = ""; txtEmailType10.Text = email_type; txtMergeVars10.Text = "";
        }
    }
}